#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2024/9/20 17:46
# @Author  : Tom_zc
# @FileName: gitcode_helper.py
# @Software: PyCharm
import os

from .request_handler import RequestHandler


class GitCodeHelper:
    get_pr_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}?access_token={token}"
    create_label_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/labels?access_token={token}"
    delete_label_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/labels/{name}?access_token={token}"
    create_comment_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/comments?access_token={token}"
    get_comment_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/comments?access_token={token}&page={page}&per_page=100"
    create_tag_url = "{domain}/api/v5/repos/{owner}/{repo}/tags?access_token={token}"
    get_tag_url = "{domain}/api/v5/repos/{owner}/{repo}/tags?access_token={token}"
    delete_tag_url = "{domain}/api/v5/repos/{owner}/{repo}/protected_tags/{tag_name}?access_token={token}"
    create_protected_tag_url = "{domain}/api/v5/repos/{owner}/{repo}/protected_tags?access_token={token}"
    get_pr_files_url = "{domain}/api/v5/repos/{owner}/{repo}/pulls/{number}/files?access_token={token}"
    get_repo_url = "{domain}/api/v5/repos/{owner}/{repo}?access_token={token}"
    create_repo_url = "{domain}/api/v5/orgs/{org}/repos?access_token={token}"
    patch_repo_url = "{domain}/api/v5/repos/{owner}/{repo}?access_token={token}"
    get_contents_url = "{domain}/api/v5/repos/{owner}/{repo}/contents/{path}?access_token={token}"

    def __init__(self, owner, repo, number, token,
                 domain=os.getenv("gitcode_api_domain"),
                 request_handler=RequestHandler()):
        self.request_handler = request_handler
        self.owner = owner
        self.repo = repo
        self.number = number
        self.token = token
        self.domain = domain

    def get_pr(self):
        url = self.get_pr_url.format(domain=self.domain, owner=self.owner,
                                     repo=self.repo, number=self.number, token=self.token)
        return self.request_handler.get(url)

    def create_label(self, json_data):
        url = self.create_label_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, token=self.token)
        _, resp = self.request_handler.post(url, json_data)
        return resp

    def delete_label(self, label_name):
        url = self.delete_label_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, name=label_name, token=self.token)
        return self.request_handler.delete(url)

    def create_comment(self, data):
        url = self.create_comment_url.format(domain=self.domain, owner=self.owner,
                                             repo=self.repo, number=self.number, token=self.token)
        _, resp = self.request_handler.post(url, data)
        return resp

    def get_comment_by_pr_id(self):
        page = 1
        list_data = list()
        while True:
            url = self.get_comment_url.format(domain=self.domain, owner=self.owner,
                                              repo=self.repo, number=self.number,
                                              token=self.token, page=page)
            resp = self.request_handler.get(url)
            list_data.extend(resp)
            if len(resp) == 100:
                page += 1
                continue
            break
        return resp

    def get_tag(self):
        url = self.get_tag_url.format(domain=self.domain, owner=self.owner,
                                      repo=self.repo, token=self.token)
        return self.request_handler.get(url)

    def create_tag(self, data):
        url = self.create_tag_url.format(domain=self.domain, owner=self.owner,
                                         repo=self.repo, token=self.token)
        _, resp = self.request_handler.post(url, data)
        return resp

    def create_protected_tag(self, data):
        url = self.create_protected_tag_url.format(domain=self.domain, owner=self.owner,
                                                   repo=self.repo, token=self.token)
        _, resp = self.request_handler.post(url, data)
        return resp

    def get_pr_files(self):
        url = self.get_pr_files_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, number=self.number, token=self.token)
        return self.request_handler.get(url)

    def get_repo(self, repo):
        url = self.get_repo_url.format(domain=self.domain, owner=self.owner,
                                       repo=repo, token=self.token)
        return self.request_handler.get(url, is_suppress_error=True, is_return_json=False)

    def create_repo(self, body, org="openUBMC-test"):
        url = self.create_repo_url.format(domain=self.domain, org=org, token=self.token)
        return self.request_handler.post(url, body)

    def modify_repo(self, repo, body):
        url = self.patch_repo_url.format(domain=self.domain, owner=self.owner,
                                         repo=repo, token=self.token)
        return self.request_handler.patch(url, body)

    def get_contents(self, path):
        url = self.get_contents_url.format(domain=self.domain, owner=self.owner,
                                           repo=self.repo, path=path, token=self.token)
        return self.request_handler.get(url)
